package com.app.reportproblem.httpclient;

import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.app.reportproblem.Utils.PreferenceHelper;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class HttpClientHelper {
	// khai bao duong dan api chung
	private static final String BASE_URL = "http://fl.jshort.vn";
	// register
	private static final String REGISTER = "/api/register";
	private static final String LOGIN = "/api/login";
	private static final String FORGOT_PASS = "/api/forgetPass";
	
	private static final String GET_PROBLEM = "/api/getProblem";
	private static final String LOGIN_WITH_FB = "/api/loginSocial";
	private static final String FIX_PROBLEM = "/api/fixProblem";
	private static final String CHECK_LASTUPDATE = "/api/checkLastUpdate";
	
	// khai bao context
	private Context context;
		
	// khai bao cac thanh phan thao tac client va server
	private static AsyncHttpClient client = new AsyncHttpClient();
	private AsyncHttpResponseHandler  asyncHttpResponseHandler;
		
	// khai bao file luu tam
	private PreferenceHelper preferenceHelper;
		
	// contructor
	public HttpClientHelper(AsyncHttpResponseHandler asyncHttpResponseHandler, Context context) {
		this.asyncHttpResponseHandler = asyncHttpResponseHandler;
		this.context = context;
		preferenceHelper = new PreferenceHelper(context);
	}
		
	public HttpClientHelper(AsyncHttpResponseHandler asyncHttpResponseHandler) {
		this.asyncHttpResponseHandler = asyncHttpResponseHandler;
	}
		
	public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
		client.get(getAbsoluteUrl(BASE_URL, url), params, responseHandler);
	}
		
	// phan rieng
	public static void get(String url, AsyncHttpResponseHandler responseHandler) {
		client.get(url, null, responseHandler);
	}

	public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
		client.post(getAbsoluteUrl(BASE_URL,url), params, responseHandler);
	}
		
		
	// get url voi tham so truyen len
	private static String getUrlWithParams(String base, String relativeUrl, List<BasicNameValuePair> params) {
		String url = getAbsoluteUrl(base, relativeUrl);
		for (int i = 0; i < params.size(); i++) {
			BasicNameValuePair b = params.get(i);
			url+="&"+b.getName()+"="+b.getValue();
		}
		url = url.replace(" ", "%20");
		return url;
	}
		
	// get url post
	private static String getUrlPostWithParams(String base, String relativeUrl, RequestParams params) {
		String url = getAbsoluteUrl(base, relativeUrl);
		RequestParams r = params;
		url+="?"+r;
		url = url.replace(" ", "%20");
				
		return url;
	}
		
		
	// tao duong dan api 
	private static String getAbsoluteUrl(String base, String relativeUrl) {
		return base + relativeUrl + "/";
	}
		
	// register
	public void regiter(JSONObject data) {
		RequestParams params = new RequestParams();
		params.add("data", data.toString());
		post(REGISTER+"?", params, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, REGISTER, params));
	}
		
	// login
	public void login(String email, String pass) {
		RequestParams params = new RequestParams();
		params.add("email", email);
		params.add("pass", pass);
		post(LOGIN+"?", params, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, LOGIN, params));
	}
		
		// login
		public void forgotPass(String email) {
			RequestParams params = new RequestParams();
			params.add("email", email);
			post(FORGOT_PASS+"?", params, asyncHttpResponseHandler);
			Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, FORGOT_PASS, params));
		}
		
		// login
		public void getProblem(int categoryId, float lon, float lat) {
			RequestParams params = new RequestParams();
			params.add("categoryId", String.valueOf(categoryId));
			params.add("lon", String.valueOf(lon));
			params.add("lat", String.valueOf(lat));
			post(GET_PROBLEM+"?", params, asyncHttpResponseHandler);
			Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, GET_PROBLEM, params));
		}
		
		// login
		public void loginSocial(String access_code, String type) {
			RequestParams params = new RequestParams();
			params.add("access_code", access_code);
			params.add("type", type);
			post(LOGIN_WITH_FB+"?", params, asyncHttpResponseHandler);
			Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, LOGIN_WITH_FB, params));
		}

		// fix
		public void fixProblem(int userId, int type) {
			RequestParams params = new RequestParams();
			params.add("userId", String.valueOf(userId));
			params.add("reportId", String.valueOf(type));
			post(FIX_PROBLEM+"?", params, asyncHttpResponseHandler);
			Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, FIX_PROBLEM, params));
		}
		
		// check last update
		public void checkLastUpdate(String type) {
			RequestParams params = new RequestParams();
			params.add("device_type", type);
			post(CHECK_LASTUPDATE, params, asyncHttpResponseHandler);
			Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, CHECK_LASTUPDATE, params));
		}
		
}
